<?php 
/**
 * @todo 添加好友页面
 * @param friendid=>好友id method=>get
 * @author 马超
 */
define('_VIEW_MODEL_','default');
session_start();
require_once("./init.php");
$friendId = intval($friendid); //get方式传过来的好友id
$uId      = isset($_SESSION['id'])?$_SESSION['id']:null; //用户id

if(empty($uId)){ //验证没有登录禁止访问
	errorPrompt('no_login','login.php');exit;
}

if(empty($friendId) || ($friendId == $uId)){ //验证friendid是否存在 friendid和uid是否相同
	errorPrompt('param_error','index.php');exit;
}

$friendInfo = $db->getRow("select Id from {$telper}user where id='$friendId'");
if(empty($friendInfo)){ //验证好友信息是否存在
	errorPrompt('no_user','index.php');exit;
}

//添加好友
$time     = time();
//将friend 添加为 uid好友
if($rowInfo = $db->getRow("select id from {$telper}friend where userid='$uId' and friendid='$friendId'")){
	$rowId = $rowInfo['id'];
	$db->Execute("update {$telper}friend set state='0',created = '$time' where id='$rowId'");
}else{
	$db->Execute("insert into {$telper}friend (userid,friendid,created,state)Values('$uId','$friendId','$time','0') ");
}
//将uid添加为friendid好友
if($rowInfo = $db->getRow("select id from {$telper}friend where userid='$friendId' and friendid='$uId'")){
	$rowId = $rowInfo['id'];
	$db->Execute("update {$telper}friend set state='0',created = '$time' where id='$rowId'");
}else{
	$db->Execute("insert into {$telper}friend (userid,friendid,created,state)Values('$friendId','$uId','$time','0') ");
}

header("Content-type:text/vnd.wap.wml;charset=utf-8");
$smartyObj->display('addfriend.tpl');
?>